package cn.ljx.entity;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 
 * 绫绘弿杩帮細鏃堕棿鎿嶄綔瀹氫箟绫�
 * 
 * @author:  寮犱唬娴�
 * @date锛�鏃ユ湡锛�012-12-8 鏃堕棿锛氫笅鍗�2:15:03
 * @version 1.0
 */
public class DateUtils extends PropertyEditorSupport {

	// 鍚勭鏃堕棿鏍煎紡
	public static final String DATE_SDF_STR = "yyyy-MM-dd";
	public static final SimpleDateFormat date_sdf = new SimpleDateFormat(
			DATE_SDF_STR);
	// 鍚勭鏃堕棿鏍煎紡
	public static final String YYYYMMDD_STR = "yyyyMMdd";
	public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat(
			YYYYMMDD_STR);
	// 鍚勭鏃堕棿鏍煎紡
	public static final String DATE_SDF_WZ_STR = "yyyy年MM月dd日";
	public static final SimpleDateFormat date_sdf_wz = new SimpleDateFormat(
			DATE_SDF_WZ_STR);
	public static final String TIME_SDF_STR = "yyyy-MM-dd HH:mm";
	public static final SimpleDateFormat time_sdf = new SimpleDateFormat(
			TIME_SDF_STR);
	public static final String YYYYMMDDHHMMSS_STR = "yyyyMMddHHmmss";
	public static final SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat(
			YYYYMMDDHHMMSS_STR);
	public static final String SHORT_TIME_SDF_STR = "HH:mm";
	public static final SimpleDateFormat short_time_sdf = new SimpleDateFormat(
			SHORT_TIME_SDF_STR);
	public static final String DATETIMEFORMAT_STR = "yyyy-MM-dd HH:mm:ss";
	public static final  SimpleDateFormat datetimeFormat = new SimpleDateFormat(
			DATETIMEFORMAT_STR);
	// 浠ユ绉掕〃绀虹殑鏃堕棿
	private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
	private static final long HOUR_IN_MILLIS = 3600 * 1000;
	private static final long MINUTE_IN_MILLIS = 60 * 1000;
	private static final long SECOND_IN_MILLIS = 1000;
	
	// 鎸囧畾妯″紡鐨勬椂闂存牸寮�
	private static SimpleDateFormat getSDFormat(String pattern) {
		return new SimpleDateFormat(pattern);
	}

	/**
	 * 褰撳墠鏃ュ巻锛岃繖閲岀敤涓浗鏃堕棿琛ㄧず
	 * 
	 * @return 浠ュ綋鍦版椂鍖鸿〃绀虹殑绯荤粺褰撳墠鏃ュ巻
	 */
	public static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	/**
	 * 鎸囧畾姣鏁拌〃绀虹殑鏃ュ巻
	 * 
	 * @param millis
	 *            姣鏁�
	 * @return 鎸囧畾姣鏁拌〃绀虹殑鏃ュ巻
	 */
	public static Calendar getCalendar(long millis) {
		Calendar cal = Calendar.getInstance();
		// --------------------cal.setTimeInMillis(millis);
		cal.setTime(new Date(millis));
		return cal;
	}

	// ////////////////////////////////////////////////////////////////////////////
	// getDate
	// 鍚勭鏂瑰紡鑾峰彇鐨凞ate
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 褰撳墠鏃ユ湡
	 * 
	 * @return 绯荤粺褰撳墠鏃堕棿
	 */
	public static Date getDate() {
		return new Date();
	}

	/**
	 * 鎸囧畾姣鏁拌〃绀虹殑鏃ユ湡
	 * 
	 * @param millis
	 *            姣鏁�
	 * @return 鎸囧畾姣鏁拌〃绀虹殑鏃ユ湡
	 */
	public static Date getDate(long millis) {
		return new Date(millis);
	}

	/**
	 * 鏃堕棿鎴宠浆鎹负瀛楃涓�
	 * 
	 * @param time
	 * @return
	 */
	public static String timestamptoStr(Timestamp time) {
		Date date = null;
		if (null != time) {
			date = new Date(time.getTime());
		}
		return date2Str(date_sdf);
	}

	/**
	 * 瀛楃涓茶浆鎹㈡椂闂存埑
	 * 
	 * @param str
	 * @return
	 */
	public static Timestamp str2Timestamp(String str) {
		Date date = str2Date(str, date_sdf);
		return new Timestamp(date.getTime());
	}
	/**
	 * 瀛楃涓茶浆鎹㈡垚鏃ユ湡
	 * @param str
	 * @param sdf
	 * @return
	 */
	public static Date str2Date(String str, SimpleDateFormat sdf) {
		if (null == str || "".equals(str)) {
			return null;
		}
		Date date = null;
		try {
			date = sdf.parse(str);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 鏃ユ湡杞崲涓哄瓧绗︿覆
	 * 
	 * @param date
	 *            鏃ユ湡
	 * @param format
	 *            鏃ユ湡鏍煎紡
	 * @return 瀛楃涓�
	 */
	public static String date2Str(SimpleDateFormat date_sdf) {
		Date date=getDate();
		if (null == date) {
			return null;
		}
		return date_sdf.format(date);
	}
	
	/**
	 * 鏃ユ湡杞崲涓哄瓧绗︿覆
	 * 
	 * @param date
	 *            鏃ユ湡
	 * @param format
	 *            鏃ユ湡鏍煎紡
	 * @return 瀛楃涓�
	 */
	public static String date2Str(Date date, SimpleDateFormat date_sdf) {
		if (null == date) {
			return null;
		}
		return date_sdf.format(date);
	}
	/**
	 * 鏃ユ湡杞崲涓哄瓧绗︿覆
	 * 
	 * @param date
	 *            鏃ユ湡
	 * @param format
	 *            鏃ユ湡鏍煎紡
	 * @return 瀛楃涓�
	 */
	public static String getDate(String format) {
		Date date=new Date();
		if (null == date) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 鎸囧畾姣鏁扮殑鏃堕棿鎴�
	 * 
	 * @param millis
	 *            姣鏁�
	 * @return 鎸囧畾姣鏁扮殑鏃堕棿鎴�
	 */
	public static Timestamp getTimestamp(long millis) {
		return new Timestamp(millis);
	}

	/**
	 * 浠ュ瓧绗﹀舰寮忚〃绀虹殑鏃堕棿鎴�
	 * 
	 * @param time
	 *            姣鏁�
	 * @return 浠ュ瓧绗﹀舰寮忚〃绀虹殑鏃堕棿鎴�
	 */
	public static Timestamp getTimestamp(String time) {
		return new Timestamp(Long.parseLong(time));
	}

	/**
	 * 绯荤粺褰撳墠鐨勬椂闂存埑
	 * 
	 * @return 绯荤粺褰撳墠鐨勬椂闂存埑
	 */
	public static Timestamp getTimestamp() {
		return new Timestamp(new Date().getTime());
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勬椂闂存埑
	 * 
	 * @param date
	 *            鎸囧畾鏃ユ湡
	 * @return 鎸囧畾鏃ユ湡鐨勬椂闂存埑
	 */
	public static Timestamp getTimestamp(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * 鎸囧畾鏃ュ巻鐨勬椂闂存埑
	 * 
	 * @param cal
	 *            鎸囧畾鏃ュ巻
	 * @return 鎸囧畾鏃ュ巻鐨勬椂闂存埑
	 */
	public static Timestamp getCalendarTimestamp(Calendar cal) {
		// ---------------------return new Timestamp(cal.getTimeInMillis());
		return new Timestamp(cal.getTime().getTime());
	}

	public static Timestamp gettimestamp() {
		Date dt = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = df.format(dt);
		java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
		return buydate;
	}

	// ////////////////////////////////////////////////////////////////////////////
	// getMillis
	// 鍚勭鏂瑰紡鑾峰彇鐨凪illis
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 绯荤粺鏃堕棿鐨勬绉掓暟
	 * 
	 * @return 绯荤粺鏃堕棿鐨勬绉掓暟
	 */
	public static long getMillis() {
		return new Date().getTime();
	}

	/**
	 * 鎸囧畾鏃ュ巻鐨勬绉掓暟
	 * 
	 * @param cal
	 *            鎸囧畾鏃ュ巻
	 * @return 鎸囧畾鏃ュ巻鐨勬绉掓暟
	 */
	public static long getMillis(Calendar cal) {
		// --------------------return cal.getTimeInMillis();
		return cal.getTime().getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勬绉掓暟
	 * 
	 * @param date
	 *            鎸囧畾鏃ユ湡
	 * @return 鎸囧畾鏃ユ湡鐨勬绉掓暟
	 */
	public static long getMillis(Date date) {
		return date.getTime();
	}

	/**
	 * 鎸囧畾鏃堕棿鎴崇殑姣鏁�
	 * 
	 * @param ts
	 *            鎸囧畾鏃堕棿鎴�
	 * @return 鎸囧畾鏃堕棿鎴崇殑姣鏁�
	 */
	public static long getMillis(Timestamp ts) {
		return ts.getTime();
	}

	// ////////////////////////////////////////////////////////////////////////////
	// formatDate
	// 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氬勾-鏈�鏃�
	 * 
	 * @return 榛樿鏃ユ湡鎸夆�骞�鏈�鏃モ�鏍煎紡鏄剧ず
	 */
	public static String formatDate() {
		return date_sdf.format(getCalendar().getTime());
	}
	/**
	 * 鑾峰彇鏃堕棿瀛楃涓�
	 */
	public static String getDateString(SimpleDateFormat formatstr) {
		return formatstr.format(getCalendar().getTime());
	}
	/**
	 * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�鏃�
	 * 
	 * @param cal
	 *            鎸囧畾鐨勬棩鏈�
	 * @return 鎸囧畾鏃ユ湡鎸夆�骞�鏈�鏃モ�鏍煎紡鏄剧ず
	 */
	public static String formatDate(Calendar cal) {
		return date_sdf.format(cal.getTime());
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�鏃�
	 * 
	 * @param date
	 *            鎸囧畾鐨勬棩鏈�
	 * @return 鎸囧畾鏃ユ湡鎸夆�骞�鏈�鏃モ�鏍煎紡鏄剧ず
	 */
	public static String formatDate(Date date) {
		return date_sdf.format(date);
	}

	/**
	 * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細骞�鏈�鏃�
	 * 
	 * @param millis
	 *            鎸囧畾鐨勬绉掓暟
	 * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滃勾-鏈�鏃モ�鏍煎紡鏄剧ず
	 */
	public static String formatDate(long millis) {
		return date_sdf.format(new Date(millis));
	}

	/**
	 * 榛樿鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀�
	 * 
	 * @param pattern
	 *            鎸囧畾鐨勬牸寮�
	 * @return 榛樿鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀�
	 */
	public static String formatDate(String pattern) {
		return getSDFormat(pattern).format(getCalendar().getTime());
	}

	/**
	 * 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀�
	 * 
	 * @param cal
	 *            鎸囧畾鐨勬棩鏈�
	 * @param pattern
	 *            鎸囧畾鐨勬牸寮�
	 * @return 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀�
	 */
	public static String formatDate(Calendar cal, String pattern) {
		return getSDFormat(pattern).format(cal.getTime());
	}

	/**
	 * 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀�
	 * 
	 * @param date
	 *            鎸囧畾鐨勬棩鏈�
	 * @param pattern
	 *            鎸囧畾鐨勬牸寮�
	 * @return 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀�
	 */
	public static String formatDate(Date date, String pattern) {
		return getSDFormat(pattern).format(date);
	}

	// ////////////////////////////////////////////////////////////////////////////
	// formatTime
	// 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氬勾-鏈�鏃�鏃讹細鍒�
	 * 
	 * @return 榛樿鏃ユ湡鎸夆�骞�鏈�鏃�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatTime() {
		return time_sdf.format(getCalendar().getTime());
	}

	/**
	 * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細骞�鏈�鏃�鏃讹細鍒�
	 * 
	 * @param millis
	 *            鎸囧畾鐨勬绉掓暟
	 * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滃勾-鏈�鏃�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatTime(long millis) {
		return time_sdf.format(new Date(millis));
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�鏃�鏃讹細鍒�
	 * 
	 * @param cal
	 *            鎸囧畾鐨勬棩鏈�
	 * @return 鎸囧畾鏃ユ湡鎸夆�骞�鏈�鏃�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatTime(Calendar cal) {
		return time_sdf.format(cal.getTime());
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�鏃�鏃讹細鍒�
	 * 
	 * @param date
	 *            鎸囧畾鐨勬棩鏈�
	 * @return 鎸囧畾鏃ユ湡鎸夆�骞�鏈�鏃�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatTime(Date date) {
		return time_sdf.format(date);
	}

	// ////////////////////////////////////////////////////////////////////////////
	// formatShortTime
	// 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氭椂锛氬垎
	 * 
	 * @return 榛樿鏃ユ湡鎸夆�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatShortTime() {
		return short_time_sdf.format(getCalendar().getTime());
	}

	/**
	 * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細鏃讹細鍒�
	 * 
	 * @param millis
	 *            鎸囧畾鐨勬绉掓暟
	 * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滄椂锛氬垎鈥滄牸寮忔樉绀�
	 */
	public static String formatShortTime(long millis) {
		return short_time_sdf.format(new Date(millis));
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氭椂锛氬垎
	 * 
	 * @param cal
	 *            鎸囧畾鐨勬棩鏈�
	 * @return 鎸囧畾鏃ユ湡鎸夆�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatShortTime(Calendar cal) {
		return short_time_sdf.format(cal.getTime());
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氭椂锛氬垎
	 * 
	 * @param date
	 *            鎸囧畾鐨勬棩鏈�
	 * @return 鎸囧畾鏃ユ湡鎸夆�鏃讹細鍒嗏�鏍煎紡鏄剧ず
	 */
	public static String formatShortTime(Date date) {
		return short_time_sdf.format(date);
	}

	// ////////////////////////////////////////////////////////////////////////////
	// parseDate
	// parseCalendar
	// parseTimestamp
	// 灏嗗瓧绗︿覆鎸夌収涓�畾鐨勬牸寮忚浆鍖栦负鏃ユ湡鎴栨椂闂�
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂�
	 * 
	 * @param src
	 *            灏嗚杞崲鐨勫師濮嬪瓧绗︾獪
	 * @param pattern
	 *            杞崲鐨勫尮閰嶆牸寮�
	 * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬棩鏈�
	 * @throws ParseException
	 * @throws AIDateFormatException
	 */
	public static Date parseDate(String src, String pattern)
			throws ParseException {
		return getSDFormat(pattern).parse(src);

	}

	/**
	 * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂�
	 * 
	 * @param src
	 *            灏嗚杞崲鐨勫師濮嬪瓧绗︾獪
	 * @param pattern
	 *            杞崲鐨勫尮閰嶆牸寮�
	 * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬棩鏈�
	 * @throws ParseException
	 * @throws AIDateFormatException
	 */
	public static Calendar parseCalendar(String src, String pattern)
			throws ParseException {

		Date date = parseDate(src, pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	public static String formatAddDate(String src, String pattern, int amount)
			throws ParseException {
		Calendar cal;
		cal = parseCalendar(src, pattern);
		cal.add(Calendar.DATE, amount);
		return formatDate(cal);
	}

	/**
	 * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂�
	 * 
	 * @param src
	 *            灏嗚杞崲鐨勫師濮嬪瓧绗︾獪
	 * @param pattern
	 *            杞崲鐨勫尮閰嶆牸寮�
	 * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬椂闂存埑
	 * @throws ParseException
	 * @throws AIDateFormatException
	 */
	public static Timestamp parseTimestamp(String src, String pattern)
			throws ParseException {
		Date date = parseDate(src, pattern);
		return new Timestamp(date.getTime());
	}

	// ////////////////////////////////////////////////////////////////////////////
	// dateDiff
	// 璁＄畻涓や釜鏃ユ湡涔嬮棿鐨勫樊鍊�
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 璁＄畻涓や釜鏃堕棿涔嬮棿鐨勫樊鍊硷紝鏍规嵁鏍囧織鐨勪笉鍚岃�涓嶅悓
	 * 
	 * @param flag
	 *            璁＄畻鏍囧織锛岃〃绀烘寜鐓у勾/鏈�鏃�鏃�鍒�绉掔瓑璁＄畻
	 * @param calSrc
	 *            鍑忔暟
	 * @param calDes
	 *            琚噺鏁�
	 * @return 涓や釜鏃ユ湡涔嬮棿鐨勫樊鍊�
	 */
	public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {

		long millisDiff = getMillis(calSrc) - getMillis(calDes);

		if (flag == 'y') {
			return (calSrc.get(calSrc.YEAR) - calDes.get(calDes.YEAR));
		}

		if (flag == 'd') {
			return (int) (millisDiff / DAY_IN_MILLIS);
		}

		if (flag == 'h') {
			return (int) (millisDiff / HOUR_IN_MILLIS);
		}

		if (flag == 'm') {
			return (int) (millisDiff / MINUTE_IN_MILLIS);
		}

		if (flag == 's') {
			return (int) (millisDiff / SECOND_IN_MILLIS);
		}

		return 0;
	}
    /**
     * String绫诲瀷 杞崲涓篋ate,
     * 濡傛灉鍙傛暟闀垮害涓�0 杞崲鏍煎紡鈥漼yyy-MM-dd鈥�
     *濡傛灉鍙傛暟闀垮害涓�9 杞崲鏍煎紡鈥漼yyy-MM-dd HH:mm:ss鈥�
     * * @param text
	 *             String绫诲瀷鐨勬椂闂村�
     */
	public void setAsText(String text) throws IllegalArgumentException {
		if (text != null && !"".equals(text)) {
			try {
				if (text.indexOf(":") == -1 && text.length() == 10) {
					setValue(this.date_sdf.parse(text));
				} else if (text.indexOf(":") > 0 && text.length() == 19) {
					setValue(this.datetimeFormat.parse(text));
				} else {
					throw new IllegalArgumentException(
							"Could not parse date, date format is error ");
				}
			} catch (ParseException ex) {
				IllegalArgumentException iae = new IllegalArgumentException(
						"Could not parse date: " + ex.getMessage());
				iae.initCause(ex);
				throw iae;
			}
		} else {
			setValue(null);
		}
	}
	
	public static int getYear(){
	    GregorianCalendar calendar=new GregorianCalendar();
	    calendar.setTime(getDate());
	    return calendar.get(Calendar.YEAR);
	  }
	public static List<String> findDates(Date dBegin, Date dEnd) {  
        List lDate = new ArrayList();  
        lDate.add(formatDate(dBegin));  
        Calendar calBegin = Calendar.getInstance();  
        // 浣跨敤缁欏畾鐨�Date 璁剧疆姝�Calendar 鐨勬椂闂�   
        calBegin.setTime(dBegin);  
        Calendar calEnd = Calendar.getInstance();  
        // 浣跨敤缁欏畾鐨�Date 璁剧疆姝�Calendar 鐨勬椂闂�   
        calEnd.setTime(dEnd);  
        // 娴嬭瘯姝ゆ棩鏈熸槸鍚﹀湪鎸囧畾鏃ユ湡涔嬪悗    
        while (dEnd.after(calBegin.getTime())) {  
            // 鏍规嵁鏃ュ巻鐨勮鍒欙紝涓虹粰瀹氱殑鏃ュ巻瀛楁娣诲姞鎴栧噺鍘绘寚瀹氱殑鏃堕棿閲�   
            calBegin.add(Calendar.DAY_OF_MONTH, 1);  
            lDate.add(formatDate(calBegin.getTime()));  
        }  
        return lDate;  
    }
    //鑾峰彇褰撳ぉ鐨勫紑濮嬫椂闂�
    public static java.util.Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    //鑾峰彇褰撳ぉ鐨勭粨鏉熸椂闂�
    public static java.util.Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    //鑾峰彇鏄ㄥぉ鐨勫紑濮嬫椂闂�
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //鑾峰彇鏄ㄥぉ鐨勭粨鏉熸椂闂�
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //鑾峰彇鏄庡ぉ鐨勫紑濮嬫椂闂�
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }
    //鑾峰彇鏄庡ぉ鐨勭粨鏉熸椂闂�
    public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }
    //鑾峰彇鏈懆鐨勫紑濮嬫椂闂�
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }
    //鑾峰彇鏈懆鐨勭粨鏉熸椂闂�
    public static Date getEndDayOfWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());  
        cal.add(Calendar.DAY_OF_WEEK, 6); 
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }
    //鑾峰彇鏈湀鐨勫紑濮嬫椂闂�
     public static Date getBeginDayOfMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getNowYear(), getNowMonth() - 1, 1);
            return getDayStartTime(calendar.getTime());
        }
    //鑾峰彇鏈湀鐨勭粨鏉熸椂闂�
     public static Date getEndDayOfMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getNowYear(), getNowMonth() - 1, 1);
            int day = calendar.getActualMaximum(5);
            calendar.set(getNowYear(), getNowMonth() - 1, day);
            return getDayEndTime(calendar.getTime());
        }
     //鑾峰彇鏈勾鐨勫紑濮嬫椂闂�
     public static java.util.Date getBeginDayOfYear() {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, getNowYear());
            // cal.set
            cal.set(Calendar.MONTH, Calendar.JANUARY);
            cal.set(Calendar.DATE, 1);

            return getDayStartTime(cal.getTime());
        }
     //鑾峰彇鏈勾鐨勭粨鏉熸椂闂�
     public static java.util.Date getEndDayOfYear() {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, getNowYear());
            cal.set(Calendar.MONTH, Calendar.DECEMBER);
            cal.set(Calendar.DATE, 31);
            return getDayEndTime(cal.getTime());
        }
    //鑾峰彇鏌愪釜鏃ユ湡鐨勫紑濮嬫椂闂�
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }
    //鑾峰彇鏌愪釜鏃ユ湡鐨勭粨鏉熸椂闂�
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
      //  calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }
    //鑾峰彇浠婂勾鏄摢涓�勾
     public static Integer getNowYear() {
             Date date = new Date();
            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
            gc.setTime(date);
            return Integer.valueOf(gc.get(1));
        }
     //鑾峰彇鏈湀鏄摢涓�湀
     public static int getNowMonth() {
             Date date = new Date();
            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
            gc.setTime(date);
            return gc.get(2) + 1;
        }
     //涓や釜鏃ユ湡鐩稿噺寰楀埌鐨勫ぉ鏁�
     public static int getDiffDays(Date beginDate, Date endDate) {

            if (beginDate == null || endDate == null) {
                throw new IllegalArgumentException("getDiffDays param is null!");
            }

            long diff = (endDate.getTime() - beginDate.getTime())
                    / (1000 * 60 * 60 * 24);

            int days = new Long(diff).intValue();

            return days;
        }
    //涓や釜鏃ユ湡鐩稿噺寰楀埌鐨勬绉掓暟
     public static long dateDiff(Date beginDate, Date endDate) {
            long date1ms = beginDate.getTime();
            long date2ms = endDate.getTime();
            return date2ms - date1ms;
        }
     //鑾峰彇涓や釜鏃ユ湡涓殑鏈�ぇ鏃ユ湡
     public static Date max(Date beginDate, Date endDate) {
            if (beginDate == null) {
                return endDate;
            }
            if (endDate == null) {
                return beginDate;
            }
            if (beginDate.after(endDate)) {
                return beginDate;
            }
            return endDate;
        }
     //鑾峰彇涓や釜鏃ユ湡涓殑鏈�皬鏃ユ湡
     public static Date min(Date beginDate, Date endDate) {
            if (beginDate == null) {
                return endDate;
            }
            if (endDate == null) {
                return beginDate;
            }
            if (beginDate.after(endDate)) {
                return endDate;
            }
            return beginDate;
        }
     //杩斿洖鏌愭湀璇ュ搴︾殑绗竴涓湀
     public static Date getFirstSeasonDate(Date date) {
             final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int sean = SEASON[cal.get(Calendar.MONTH)];
            cal.set(Calendar.MONTH, sean * 3 - 3);
            return cal.getTime();
        }
     //杩斿洖鏌愪釜鏃ユ湡涓嬪嚑澶╃殑鏃ユ湡
     public static Date getNextDay(Date date, int i) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(date);
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
            return cal.getTime();
        }
     //杩斿洖鏌愪釜鏃ユ湡鍓嶅嚑澶╃殑鏃ユ湡
     public static Date getFrontDay(Date date, int i) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(date);
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
            return cal.getTime();
        }
     //鑾峰彇鏌愬勾鏌愭湀鍒版煇骞存煇鏈堟寜澶╃殑鍒囩墖鏃ユ湡闆嗗悎锛堥棿闅斿ぉ鏁扮殑闆嗗悎锛�
     public static List getTimeList(int beginYear, int beginMonth, int endYear,
                int endMonth, int k) {
            List list = new ArrayList();
            if (beginYear == endYear) {
                for (int j = beginMonth; j <= endMonth; j++) {
                    list.add(getTimeList(beginYear, j, k));

                }
            } else {
                {
                    for (int j = beginMonth; j < 12; j++) {
                        list.add(getTimeList(beginYear, j, k));
                    }

                    for (int i = beginYear + 1; i < endYear; i++) {
                        for (int j = 0; j < 12; j++) {
                            list.add(getTimeList(i, j, k));
                        }
                    }
                    for (int j = 0; j <= endMonth; j++) {
                        list.add(getTimeList(endYear, j, k));
                    }
                }
            }
            return list;
        }
     //鑾峰彇鏌愬勾鏌愭湀鎸夊ぉ鍒囩墖鏃ユ湡闆嗗悎锛堟煇涓湀闂撮殧澶氬皯澶╃殑鏃ユ湡闆嗗悎锛�
     public static List getTimeList(int beginYear, int beginMonth, int k) {
            List list = new ArrayList();
            Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
            int max = begincal.getActualMaximum(Calendar.DATE);
            for (int i = 1; i < max; i = i + k) {
                list.add(begincal.getTime());
                begincal.add(Calendar.DATE, k);
            }
            begincal = new GregorianCalendar(beginYear, beginMonth, max);
            list.add(begincal.getTime());
            return list;
        }
}